package com.test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yyq.adms.gf.pojo.OwnLaw;
import com.yyq.adms.util.JxlsUtil;
import com.yyq.adms.util.JxlsVo;
import com.yyq.adms.util.ValidateUtil;
import com.yyq.adms.util.ValidateVo;

import jxl.Cell;
import jxl.Range;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


public class Jxls {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		File file = new File("D:/yyq/template/获批工法.xls");
		//testJxls(file);
		Map<String,ValidateVo> vMap = ValidateUtil.getInstance().getValidatesByClassName(OwnLaw.class.getName());
		List<Map<String, JxlsVo>> data = JxlsUtil.getInstance().transfromData(file);
		List list = new ArrayList();
		//JxlsUtil.getInstance().validateXls(data, vMap, OwnLaw.class, list);
		
		System.out.println(list);
		
		
		
	}

	
	private static  String getCellContent(Sheet sheet, Cell cell)
	{
		String content = cell.getContents();
		if(content==null||"".equals(content))
		{
			content = getContentFromMargeCell(sheet,cell);
		}
		return content;
	}
	
	private static String getContentFromMargeCell(Sheet sheet, Cell cell)
	{
		String content= null;
		Range[] ranges = sheet.getMergedCells();
		for(int i=0;i<ranges.length;i++)
		{
			Cell topLeft = ranges[i].getTopLeft();
			Cell bottomRight = ranges[i].getBottomRight();
			int topLeft_col = topLeft.getColumn();
			int topLeft_row = topLeft.getRow();
			int bottomRight_col = bottomRight.getColumn();
			int bottomRight_row = bottomRight.getRow();
			int col = cell.getColumn();
			int row = cell.getRow();
			if(col>=topLeft_col&&row>=topLeft_row&&col<=bottomRight_col&&row<=bottomRight_row)
			{
				content = topLeft.getContents();
				break;
			}
		}
		return content;
	}
	
	public static void testJxls(File file){
		Workbook wb = null;
		try {
			wb = Workbook.getWorkbook(file);
			int sheetCount = wb.getNumberOfSheets();
			if(sheetCount>0){
				Sheet sheet = wb.getSheet(0);
				Cell ownLawTitleCell = sheet.findCell("工法名称");
				Cell majorDep1TitleCell = sheet.findCell("主要完成单位(内部)");
				Cell majorDep2TitleCell = sheet.findCell("主要完成单位(外部)");
				Cell majorPersonTitleCell = sheet.findCell("主要完成人");
				Cell levelsTitleCell = sheet.findCell("工法级别");
				Cell lawCodeTitleCell = sheet.findCell("工法编号");
				Cell lawYearTitleCell = sheet.findCell("工法年度");
				Cell belongPlateFormIDTitleCell = sheet.findCell("所属平台");
				int row = sheet.getRows();
				if(ownLawTitleCell != null){
					for(int j=ownLawTitleCell.getRow()+1;j<row;j++){
						Cell ownLawCell = sheet.getCell(ownLawTitleCell.getColumn(), j);
						String ownLawName = getCellContent(sheet, ownLawCell);
						Cell majorDep1Cell = sheet.getCell(majorDep1TitleCell.getColumn(), j);
						String majorDep1 = getCellContent(sheet, majorDep1Cell);
						Cell majorDep2Cell = sheet.getCell(majorDep2TitleCell.getColumn(), j);
						String majorDep2 = getCellContent(sheet, majorDep2Cell);
						Cell majorPersonCell = sheet.getCell(majorPersonTitleCell.getColumn(), j);
						String majorPerson = getCellContent(sheet, majorPersonCell);
						Cell levelsCell = sheet.getCell(levelsTitleCell.getColumn(), j);
						String levels = getCellContent(sheet, levelsCell);
						Cell lawCodeCell = sheet.getCell(lawCodeTitleCell.getColumn(), j);
						String lawCode = getCellContent(sheet, lawCodeCell);
						Cell lawYearCell = sheet.getCell(lawYearTitleCell.getColumn(), j);
						String lawYear = getCellContent(sheet, lawYearCell);
						Cell belongPlateFormIDCell = sheet.getCell(belongPlateFormIDTitleCell.getColumn(), j);
						String belongPlateFormID = getCellContent(sheet, belongPlateFormIDCell);
						System.out.println(ownLawName+"| "+majorDep1+"| "+majorDep2+"| "+majorPerson+"| "+levels+"| "+lawCode+"| "+lawYear+"| "+belongPlateFormID);
					}
				}
				
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
